﻿public class NetHandler_LikeMoment : GameServerMessageHandler
{
    public const string Key = "LikeMoment";

    public static NetHandler_LikeMoment NewHandler()
    {
        return new NetHandler_LikeMoment();
    }

    protected override ISerialize NewData() => new LikeMomentRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        LikeMomentRequestData requestData = _data as LikeMomentRequestData;

        GameDebug.Log($"{client.userID} like req: {requestData.momentUserId}  {requestData?.momentId} likeOrNot:{requestData.likeOrNot}");

        UserServer user = Game.instance.GetUser(client.userID);

        ResponseData response = new ResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            long requestUser = user.data.id;

            if (requestData != null)
            {
                if (requestData.momentUserId > 0)
                {
                    long opponent = requestData.momentUserId;

                    UserServer opponentUser = Game.instance.GetUser(opponent);
                    if (opponentUser != null)
                    {
                        var moment = opponentUser.data.moments.Find(e => e.momentId == requestData.momentId);
                        if (moment != null)
                        {
                            if(requestData.likeOrNot)
                                moment.AddLike(requestUser);
                            else
                                moment.RemoveLike(requestUser);

                            GameDebug.Log($"给{opponent} 发送Like Moment结果事件");

                            NetEvent_LikeMoment netEvent_Like = new NetEvent_LikeMoment();
                            netEvent_Like.likeData = new LikeMomentData();
                            netEvent_Like.likeData.likes = moment.likes;
                            netEvent_Like.likeData.momentId = requestData.momentId;
                            netEvent_Like.SendToRemote(opponentUser.client);
                        }
                        else
                        {
                            response.code = ErrorCode.CantFindMoment;
                        }

                    }

                    moments momentsTable = UserServer.FindInMomentTable(opponent, requestData.momentId);
                    if (momentsTable != null)
                    {
                        var likes = users.String2LongList(momentsTable.likes);

                        if (requestData.likeOrNot)
                        {
                            if (!likes.Contains(requestUser))
                                likes.Add(requestUser);
                        }
                        else
                        {
                            likes.Remove(requestUser);
                        }
                        likes.Remove(0);
                        int ret1 = moments.UpdateLikes(opponent,requestData.momentId, likes.ToArray());

                        if (ret1 >= 0)
                            response.code = 0;
                        else
                            response.code = -1;
                    }
                    else
                    {
                        response.code = ErrorCode.CantFindMoment;
                    }

                }
                else
                {
                    response.code = ErrorCode.InvalidUserId;
                }
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}


